﻿using Competition.App.Web.Models.ProductViewModels;
using Competition.App.Web.ResultModels.ProductResultModels;
using Competition.Application.DTO;
using Competition.Application.Interfaces;
using Competition.Infrastructure;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Competition.Domain.ValueObjects;
using Competition.Infrastructure.Config;

namespace Competition.App.Web.Controllers
{
    [DisableRequestSizeLimit]
    public class ProductController : Controller
    {
        private readonly Lazy<IProductService> _productService;

        public ProductController(Lazy<IProductService> productService)
        {
            _productService = productService;
        }
        // GET: News
        public async Task<IActionResult> Index(int page)
        {
            if (page < 1)
            {
                page = 1;
            }

            var v = await _productService.Value.FindListAsync(page, 5);

            List<ProductResultModel> models = v.dtos.Select(dto => new ProductResultModel()
            {
                Id = dto.Id,
                AddTime = dto.AddTime.ToString("yyyy-MM-dd"),
                Title = dto.Title,
                Description = dto.Description,
                Cover = AppConfig.OtherConfig.AdminUrl + dto.Cover,
                ClassId = dto.ClassId.ToString()
            }).ToList();

            ViewBag.List = models;
            ViewBag.TotalData = v.pageDto.TotalRecord;
            ViewBag.CurrentPage = page;  //当前页
            return View();
        }

        // GET: News/Details/5
        public async Task<IActionResult> Details(int id)
        {
            var dto = await _productService.Value.FindAsync(id);
            ProductViewModel model = new ProductViewModel()
            {
                Cover = dto.Cover,
                Description = dto.Description,
                AddTime = dto.AddTime.ToString("yyyy.M.d"),
                Title = dto.Title,
                Id = dto.Id,
                Annex = dto.Annex,
                Html = dto.Html
                
            };
            return View(nameof(Details),model);
        }

        public IActionResult Create()
        {
            return View(nameof(Create));
        }
        // POST: News/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create([FromForm]ProductViewModel model, [FromForm]IFormFile file)
        {
            if (file == null)
            {
                ModelState.AddModelError(nameof(model.Annex), "必填");
            }
            if (ModelState.IsValid)
            {
                var dto = new ProductDto()
                {
                    FullName = model.FullName,
                    Identity = model.Identity,
                    Phone = model.Phone,
                    Title = model.Title,
                    Annex = UploadHelper.UploadDownload(file),
                    ClassId = (ProductClassEnum)model.ClassId
                };
               await _productService.Value.AddAsync(dto);
                return Redirect("~/jump.html");
            }

            return View(nameof(Create), model);
        }
    }
}